<template>
	<view class="main mainColumn mainAlignCenter" style="margin-top: 25%;">
		<view class="button1 textwbold text36" style="background-color: #5ED1A9;margin-top: 150rpx;"
			@click="getUserInfoClick" lang="zh_CN" open-type="getUserInfo" scope="userInfo">
			微信授权登录
		</view>
		<view class="button1 textwbold text36 mt24" style="color:#989898;background-color: #F4F4F4;" @click="goback"
			lang="zh_CN">
			取消
		</view>
		<u-checkbox-group class="fcenter mt32">
			<u-checkbox :label-size="labelsize" name="点餐" active-color="red" shape="circle" v-model="checked"
				:disabled="false"><text style="font-size: 24rpx;"
					@click="$u.route('pages/login/agreement')">阅读并同意用户协议和隐私政策</text>
			</u-checkbox>
		</u-checkbox-group>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		genTestUserSig
	} from '../../debug/GenerateTestUserSig.js';
	export default {
		data() {
			return {
				checked: false,
				labelsize: 20,
				datalist:[]
			}
		},
		methods: {
			//拉取授权
			getUserInfoClick(e) {
				var that = this;
				if (!that.checked) {
					that.$refs.uToast.show({
						title: '请先同意同意用户协议和隐私政策',
						type: 'error',
					})
					return
				}
				uni.getUserProfile({
					desc: '用于完善用户资料',
					lang: 'zh_CN',
					success: function(res) {
						that.getUserInfo(res);
					},
					fail: function(res) {
						console.log('wx.getUserProfile=>用户拒绝了授权');
						// console.log(res);
					},
				});
			},
			// 登录
			getUserInfo(userInfoResult) {
				// console.log(userInfoResult)
				var that = this;
				uni.login({
					scopes: 'auth_base',
					success(loginResult) {
						// console.log(loginResult);
						const data = {
							encryptedData: userInfoResult.encryptedData,
							iv: userInfoResult.iv,
							rawData: userInfoResult.rawData,
							signature: userInfoResult.signature,
							code: loginResult.code,
						};
						// console.log(data);
						that.$u.api.login(
							data
						).then(res => {

							// 完成 im 的登录，使用的 系统里面的账号登录 im系统（腾讯im自带一套登录体系）
							// let userID = res.data.user_id + ''; // im 系统规定 userID 必须是字符串
							let userID = '8888888';  //模拟第一次登录
							let userSig = genTestUserSig(userID).userSig;

							uni.$TUIKit.login({
								userID: userID,
								userSig: userSig
							}).then(() => {
								console.log('im 系统登录 成功');
							}).catch((error) => {
								console.log('im 系统登录 失败');
							})


							// // 获取用户的基本信息
							// let userInfo = this.$u.api.getUserInfo();
							// console.log('userInfo用户的基本信息', userInfo.data);

							// this.$u.vuex('userInfo', userInfo.data);

							
							// this.$u.api.user_info({}).then(res => {
							// 	this.datalist = res.data;
							// 	// console.log(1451,self.datalist)
							// 	console.log('===========获取用户信息', this.datalist);
							// });



							if (res.data.is_bind_mobile == false) {
								uni.redirectTo({
									url: '/pages/login/bindPhone' + "?user_id=" + res.data
										.user_id +
										"&token=" + res.data.access_token
								})
								return
							} else {
								that.$u.vuex('token', res.data.access_token)

								that.$u.route({
									type: 'back'
								});
							}
							
							
							
						}).catch(err => {
							this.$refs.uToast.show({
								title: err.msg,
								type: 'error'
							})
							uni.stopPullDownRefresh()
						})
					},
					fail(error) {
						reject(error);
					}
				});
			},
			codelogin() {
				console.log("654+564546")
				uni.navigateTo({
					url: './VerificationCodeLogin'
				})
			},
			goback() {
				setTimeout(() => {
					this.$u.route({
						type: 'switchTab',
						url: 'pages/tabBar/home',
						params: {
							name: 'lisa',
						},
					})
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background: #F8FAFC;
	}
</style>
